package com.specialized.orders.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.specialized.orders.common.domain.Distance;
import com.specialized.orders.common.domain.PickAirport;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author：djh
 * @Package：com.specialized.orders.utils
 * @Project：Specialized-Cloud
 * @name：ChargRule
 * @Date：2024/7/31 16:48
 */
@Component
@Log4j2
public class ChargRule {

    private static final double BASIC_FESS = 30.0; //基础费
    private static final double STARTING_PRICE = 15.0; //起步价
    private static final double TIME_FEE_MINUTE = 0.3;  //每分钟时长费
    private static final double DISTANCE_FEE_KM = 2.4; //每公里里程费
    private static final double FAR_WAY = 1.25; //远途费
    private static final double NIGHT_SERVICE  = 0.8; //夜间服务费
    private static final double TIME_PRICE  = 0.3; //元/分钟

    public Double computeMoneyrule(PickAirport pickAirport) {
        //当天23点
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.HOUR_OF_DAY,23);
        instance.set(Calendar.MINUTE,0);
        instance.set(Calendar.SECOND,0);
        Date time = instance.getTime();
        //第二天凌晨6点
        Calendar instance1 = Calendar.getInstance();
        instance1.set(Calendar.HOUR_OF_DAY,6);
        instance1.set(Calendar.DAY_OF_MONTH,(Calendar.DATE+1));
        instance1.set(Calendar.MINUTE,0);
        instance1.set(Calendar.SECOND,0);
        Date time1 = instance1.getTime();
        //判断下单时间是否夜间
        boolean date = DateUtil.isIn(pickAirport.getCreateTime(), time, time1);
        log.info(date);
        String start = pickAirport.getStartPointLongitude() + "," + pickAirport.getStartPointLatitude();
        String end = pickAirport.getDestinationLongitude() + "," + pickAirport.getDestinationLatitude();
        Distance distance = this.computeDistance(start, end);
        //公里数
        Integer distant = (distance.getRoute().getPaths().get(0).getDistance()) / 1000;
        //时长
        Integer minute = (distance.getRoute().getPaths().get(0).getDuration()) / 60;
        pickAirport.setDistance(Long.valueOf(distant));
        if(date){
            Double price = computePrice(pickAirport, minute);
            return price;
        }else {
            Double v = computeMoney(pickAirport, minute);
            return v;
        }
    }

    //夜间
    private Double computePrice(PickAirport pickAirport,Integer minute) {
        double money = BASIC_FESS + STARTING_PRICE + NIGHT_SERVICE*pickAirport.getDistance() + TIME_PRICE*minute;
        //判断时间是否大于起步价时间
        if (minute>5){
            Integer realminute = minute - 5;
            money+= (realminute*TIME_FEE_MINUTE);
        }
        Long distance = pickAirport.getDistance();
        if (distance>2 && distance<15){
            long realKlong = distance - 2;
             money += realKlong*2.4;
        }
        //判断是否远途
        if (distance>15){
            long realknowg = distance - 15;
           money += realknowg * 1.25;
        }
        return money;
    }

    //非夜间
    private Double computeMoney(PickAirport pickAirport,Integer minute) {
        double money = BASIC_FESS + STARTING_PRICE;
        //判断时间是否大于起步价时间
        if (minute>5){
            Integer realminute = minute - 5;
            money+= (realminute*TIME_FEE_MINUTE);
        }
        Long distance = pickAirport.getDistance();
        if (distance>2 && distance<15){
            long realKlong = distance - 2;
            money += realKlong*2.4;
        }
        //判断是否远途
        if (distance>15){
            long realknowg = distance - 15;
            money += realknowg * 1.25;
        }
        return money;
    }



    public static String DRIVER_URL = "https://restapi.amap.com/v3/direction/driving?";
    public static String key = "793774c415f9f7678ea8a85e54fa61b7";

    public Distance computeDistance(String origin, String destination) {
        DRIVER_URL += "key=793774c415f9f7678ea8a85e54fa61b7"+"&extensions=all"+"&origin="+origin
                +"&destination="+destination;
        String string = HttpUtil.get(DRIVER_URL);
        log.info("两地点之间距离为:"+string);
        Distance driver = JSONObject.parseObject(string, Distance.class);
        return driver;
    }


}
